//==============================================================================
//
// Title:       ElectronicLoad.c
// Purpose:     A short description of the implementation.
//
// Created on:  2012-4-26 at 15:52:51 by luh.
// Copyright:   dafda. All Rights Reserved.
//
//==============================================================================

//==============================================================================
// Include files



//#include "PSU.h"
#include "RFBB.h"
#include "cvidef.h"
//#include "ICD_PSU.h"
#include <cvintwrk.h>
#include "agtn330x.h"
#include "stdtst.h"
#include "tsutil.h"
#include <formatio.h>
#include <utility.h>
#include "ElectronicLoad.h"

static ErrMsg errMsg;
static ERRORINFO errorInfo;
static int error;
ViSession   g_vi_sInitHandleAGN3300A;
int g_iInitFailed = 0;
ViRsrc virGPIB = 0;
char name[255] = {'\0'}; 
unsigned int size = 0;


int InitAgn3300a(void)
{
   int error = 0;          // required for tsErrChk
   char* pszTempBuff = 0;
   unsigned char szDeviceAddress[255] = {'\0'};
   unsigned char szDeviceAddressFmt[] = {"GPIB0::%d::%d"};

   int i = 0;
	/* **********************************Agilent N3300A init......*******************************************/		
	EELog_INEventLogLogEvent( g_hEventLog, NULL, "Init N3300A...");

	memset( szDeviceAddress,'\0', size);       
	sprintf( name, "SOFTWARE\\NSN\\SWIFT\\Instruments\\AGN3300A");
	error = RegReadString( REGKEY_HKLM, name, "DeviceAddress", szDeviceAddress, sizeof(szDeviceAddress), &size);
	if( error ) {
      pszTempBuff = GetGeneralErrorString(error);
      goto Error;
   }
   
	virGPIB = (ViRsrc) szDeviceAddress; 
   
	error = AgtN330X_init( virGPIB, VI_TRUE, VI_FALSE, &g_vi_sInitHandleAGN3300A);
	if( error ) {
      pszTempBuff = GetGeneralErrorString(error);
      goto Error;
   }

    //disable all inputs
	for ( i=1; i<7; i++ ) {
		ErrChk( AgtN330X_setChannel( g_vi_sInitHandleAGN3300A, AgtN330X_CHAN, i) );
		ErrChk( AgtN330X_setInputState( g_vi_sInitHandleAGN3300A, VI_FALSE) );
	}
    
Error:
       if( error < 0 ) {
          g_iInitFailed = 1;
          MessagePopup("Power supply init", "Failed");          
        }
       return 0; 
}


// -----------------------------------------------------------------------
// Function:   SetupLoadCCMode
// Purpose:    
//
//
int SetupLoadCCMode(ViSession g_hInitHandleAGN3300A ,int iChannel,float dCurrent)
{
   int error = 0;	
   CAObjHandle hEventLog = 0;
   char   szLog[100] = {'\0'};
   
   ErrChk( AgtN330X_setChannel (g_hInitHandleAGN3300A, AgtN330X_CHAN, iChannel));   
   ErrChk( AgtN330X_setInputState (g_hInitHandleAGN3300A, VI_FALSE));
   ErrChk( AgtN330X_setMode (g_hInitHandleAGN3300A, AgtN330X_MODE_CURRENT));
   ErrChk( AgtN330X_setCurrent (g_hInitHandleAGN3300A, AgtN330X_CURR, dCurrent));
   sprintf( szLog, "Set Channel%d Current : %.3f",iChannel,dCurrent);
   EELog_INEventLogLogEvent (hEventLog, NULL,  szLog);   
                            
Error: 

   return error;    
}

// -----------------------------------------------------------------------
// Function:   SetInputState
// Purpose:    
//
//
int SetInputState(ViSession g_hInitHandleAGN3300A ,int iChannel,ViBoolean inputState)
{
   int error = 0;	
   CAObjHandle hEventLog = 0;
   char   szLog[100] = {'\0'};
   
   ErrChk( AgtN330X_setChannel (g_hInitHandleAGN3300A, AgtN330X_CHAN, iChannel));   
   ErrChk( AgtN330X_setInputState (g_hInitHandleAGN3300A, inputState));
   
   if(inputState == VI_TRUE)
   {
       sprintf( szLog, "Enable AGN3300A Channel%d input",iChannel);
       EELog_INEventLogLogEvent (hEventLog, NULL,  szLog);   
   }
   else
   {
       sprintf( szLog, "Disable AGN3300A Channel%d input",iChannel);
       EELog_INEventLogLogEvent (hEventLog, NULL,  szLog);
   }
   
Error: 

   return error;    
}

// -----------------------------------------------------------------------
// Function:   ReadVoltageFromOneChannelELoad
// Purpose:    
//
//
int ReadVoltageFromOneChannelELoad(ViSession g_hInitHandleAGN3300A ,int iChannel,float dDelay,double* dresult)
{
   int error = 0;    
   char   szLog[100] = {'\0'};    
	
	
   ErrChk( AgtN330X_setChannel (g_vi_sInitHandleAGN3300A, AgtN330X_CHAN, iChannel));   
   ErrChk( AgtN330X_setInputState (g_vi_sInitHandleAGN3300A, VI_TRUE));
   
   Delay( dDelay );
   
   ErrChk( AgtN330X_measureVoltage (g_vi_sInitHandleAGN3300A, AgtN330X_VOLT,dresult));   
   ErrChk( AgtN330X_setInputState (g_vi_sInitHandleAGN3300A, VI_FALSE));
   	
   sprintf( szLog, "Channel%d Voltage Measure result: %.3f",iChannel,*dresult);
   EELog_INEventLogLogEvent (g_hEventLog, NULL,  szLog);

                            
Error: 
    
   return error;    
}

// -----------------------------------------------------------------------
// Function:   ReadVoltageFromTwoChannelELoad
// Purpose:    
//
//
int ReadVoltageFromTwoChannelELoad(ViSession g_hInitHandleAGN3300A ,int iChannel,float dDelay,double* dresult)
{
   int error = 0;    
   char   szLog[100] = {'\0'};
   
   ErrChk( AgtN330X_measureVoltage (g_vi_sInitHandleAGN3300A, AgtN330X_VOLT,dresult)); 
   
   sprintf( szLog, "Channel%d Voltage Measure result: %.3f",iChannel,*dresult);
   EELog_INEventLogLogEvent (g_hEventLog, NULL,  szLog);

                            
Error: 
    
   return error;    
}



// -----------------------------------------------------------------------
// Function:   ReadVoltageELoadDisabled
// Purpose:    
//
//
int ReadVoltageELoadDisabled( ViSession g_hInitHandleAGN3300A ,int iChannel,float dDelay,double* dresult)
{
	char szLog[100] = {'\0'}; 
	error = 0;



	ErrChk( AgtN330X_setChannel (g_vi_sInitHandleAGN3300A, AgtN330X_CHAN, iChannel));       
    ErrChk( AgtN330X_setInputState( g_vi_sInitHandleAGN3300A, VI_FALSE));
    
	Delay( dDelay );
    
	ErrChk( AgtN330X_measureVoltage( g_vi_sInitHandleAGN3300A, AgtN330X_VOLT, dresult) );

	sprintf( szLog, "Channel%d Disabled ,Voltage Measure result: %.3f",iChannel, *dresult);
	EELog_INEventLogLogEvent( g_hEventLog, NULL,  szLog);
  
Error: 
    
	return error;    
}
	



int ReadELoadTestResult(ViSession g_hInitHandleAGN3300A ,int iChannel,float dCurrent,float dDelay,double* dresult)
{
	char szLog[100] = {'\0'};
	int error = 0;	
	
    //Set AgtN330X 
	ErrChk( AgtN330X_setChannel( g_vi_sInitHandleAGN3300A, AgtN330X_CHAN, iChannel) );
	ErrChk( AgtN330X_setMode( g_vi_sInitHandleAGN3300A, AgtN330X_MODE_CURRENT) );
	ErrChk( AgtN330X_setCurrent( g_vi_sInitHandleAGN3300A, AgtN330X_CURR, dCurrent) );
	ErrChk( AgtN330X_setInputState( g_vi_sInitHandleAGN3300A, VI_TRUE) );
	Delay( dDelay);

    //Read Voltage result
	ErrChk( AgtN330X_measureVoltage( g_vi_sInitHandleAGN3300A, AgtN330X_VOLT, dresult) );
	
	Delay( 1);

    //disable AgtN330X channel
    ErrChk( AgtN330X_setCurrent( g_vi_sInitHandleAGN3300A, AgtN330X_CURR, 1) );
	ErrChk( AgtN330X_setInputState( g_vi_sInitHandleAGN3300A, VI_FALSE));

	*dresult += 0.01;
	sprintf( szLog, "Channel%d ,Current %f ,Voltage Measure result: %.3f" ,iChannel,dCurrent,*dresult);
	EELog_INEventLogLogEvent( g_hEventLog, NULL,  szLog); 
                            
Error: 
    
	return error;    
}
/****************************************************************************
 *---------------------------- End Source File -----------------------------*
 ****************************************************************************/



